home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src_ansi / ace / c / alloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-21  |  4.4 KB  |  174 lines

  1. /* 
  2.    ** Unix-style hassle free memory allocation
  3.    ** via Intuition's Alloc/FreeRemember functions.
  4.    ** Copyright (C) 1998 David Benn
  5.    ** 
  6.    ** This program is free software; you can redistribute it and/or
  7.    ** modify it under the terms of the GNU General Public License
  8.    ** as published by the Free Software Foundation; either version 2
  9.    ** of the License, or (at your option) any later version.
  10.    **
  11.    ** This program is distributed in the hope that it will be useful,
  12.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    ** GNU General Public License for more details.
  15.    **
  16.    ** You should have received a copy of the GNU General Public License
  17.    ** along with this program; if not, write to the Free Software
  18.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.    **
  20.    ** Call alloc/sym_alloc for each memory allocation request.
  21.    **
  22.    ** Call free_alloc/free_sym_alloc once at the end of a program
  23.    ** run or when the symbol table for the current level is finished
  24.    ** with.
  25.    **
  26.    ** This module also contains functions for allocating and freeing 
  27.    ** memory for ACE's assembly code generation.
  28.    **
  29.    ** Author: David J Benn
  30.    **   Date: 30th June 1993, 
  31.    **      1st July 1993, 
  32.    **              16th December 1993,
  33.    **      7th,8th January 1994
  34.  */
  35.  
  36. #include "acedef.h"
  37. #include <intuition/intuition.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <clib/intuition_protos.h>
  41.  
  42. /* external variables */
  43. extern int lev;
  44.  
  45. /* local variables */
  46. struct Remember *GenRememberList = NULL;
  47. struct Remember *SymRememberList[2] =
  48. {NULL, NULL};
  49.  
  50. void free_alloc (void)
  51. {
  52. /* free all memory allocated by alloc */
  53.  
  54.   if (GenRememberList != NULL)
  55.     {
  56.       puts ("freeing memory...");
  57.       FreeRemember (&GenRememberList, TRUE);
  58.       GenRememberList = NULL;
  59.     }
  60. }
  61.  
  62. void free_sym_alloc (void)
  63. {
  64. /* free all memory allocated by sym_alloc for current level */
  65.  
  66.   if (SymRememberList[lev] != NULL)
  67.     {
  68.       FreeRemember (&SymRememberList[lev], TRUE);
  69.       SymRememberList[lev] = NULL;
  70.     }
  71. }
  72.  
  73. CODE *alloc_code (char *opcode, char *srcopr, char *destopr)
  74. {
  75. /*
  76.    ** Allocate memory for a CODE node plus its 
  77.    ** opcode, srcopr and destopr fields.
  78.    **
  79.    ** If an allocation fails, any memory 
  80.    ** allocated is freed and a NULL CODE 
  81.    ** pointer is returned.
  82.  */
  83.   CODE *cnode;
  84.   size_t opcode_size, srcopr_size, destopr_size;
  85.  
  86.   /* node */
  87.   if ((cnode = (CODE *) malloc (sizeof (CODE))) == NULL)
  88.     return (NULL);
  89.  
  90.   /* opcode,srcopr,destopr */
  91.   opcode_size = strlen (opcode) + 1;
  92.   srcopr_size = strlen (srcopr) + 1;
  93.   destopr_size = strlen (destopr) + 1;
  94.  
  95.   if ((cnode->opcode = (char *)
  96.              malloc((opcode_size+srcopr_size+destopr_size))) == NULL)
  97.     {
  98.       /* unsuccessful -> free node and abort! */
  99.       free (cnode);
  100.       return (NULL);
  101.     }
  102.  
  103.   /* set src and dest operand pointers */
  104.   cnode->srcopr = cnode->opcode + opcode_size;
  105.   cnode->destopr = cnode->srcopr + srcopr_size;
  106.  
  107.   /* return a pointer to the CODE node! */
  108.   return (cnode);
  109. }
  110.  
  111. void free_code (CODE * cnode)
  112. {
  113. /* 
  114.    ** Frees all the memory associated with
  115.    ** a CODE node, including its members.
  116.  */
  117.  
  118.   if (cnode)
  119.     {
  120.       if (cnode->opcode)
  121.     free (cnode->opcode);
  122.       free (cnode);
  123.     }
  124. }
  125.  
  126. BOOL alloc_code_members (CODE * cnode, char *opcode, char *srcopr, char *destopr)
  127. {
  128. /*
  129.    ** Allocate memory for a CODE node's
  130.    ** opcode, srcopr and destopr fields.
  131.    **
  132.    ** If the allocation fails, a boolean 
  133.    ** FALSE value is returned.
  134.  */
  135.   size_t opcode_size, srcopr_size, destopr_size;
  136.  
  137.   /* is the CODE node non-NULL? */
  138.   if (cnode == NULL)
  139.     return (FALSE);
  140.  
  141.   /* allocate opcode,srcopr,destopr */
  142.   opcode_size = strlen (opcode) + 1;
  143.   srcopr_size = strlen (srcopr) + 1;
  144.   destopr_size = strlen (destopr) + 1;
  145.  
  146.   if ((cnode->opcode = (char *)
  147.        malloc ((opcode_size + srcopr_size + destopr_size))) == NULL)
  148.     return (FALSE);
  149.  
  150.   /* set src and dest operand pointers */
  151.   cnode->srcopr = cnode->opcode + opcode_size;
  152.   cnode->destopr = cnode->srcopr + srcopr_size;
  153.  
  154.   /* we got this far so return TRUE to indicate success! */
  155.   return (TRUE);
  156. }
  157.  
  158. void free_code_members (CODE * cnode)
  159. {
  160. /* 
  161.    ** Frees all the memory associated with
  162.    ** a CODE node's members.
  163.  */
  164.  
  165.   if (cnode)
  166.     {
  167.       if (cnode->opcode)
  168.     free (cnode->opcode);
  169.       cnode->opcode = NULL;
  170.       cnode->srcopr = NULL;
  171.       cnode->destopr = NULL;
  172.     }
  173. }
  174.